package com.example.huitong.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.huitong.bean.User;
import com.example.huitong.db.DBContract.TableUser;
import com.example.huitong.db.DBHelper.Tables;

public class DBImpl implements DBDao {
	
	private static final String TAG = "DBImpl";
	private static DBImpl mInstance = null;
	private Context mContext;
	private DBHelper mDBHelper;
	private DBImpl(Context context){
		mContext = context;
		mDBHelper = new DBHelper(mContext);
	}
	public static void init(Context context){
		if(mInstance == null){
			mInstance = new DBImpl(context);
		}
	}
	public synchronized static DBImpl getInstance(){
		if(mInstance == null){
			Log.e(TAG, "You Have to INIT first!!");
		}
		return mInstance;
	}

	@Override
	public User getUser(String name, String pwd) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mDBHelper.getReadableDatabase();
		User user = null;
		// 第一个参数String：表名  
        // 第二个参数String[]:要查询的列名  
        // 第三个参数String：查询条件  
        // 第四个参数String[]：查询条件的参数  
        // 第五个参数String:对查询的结果进行分组
        // 第六个参数String：对分组的结果进行限制  
        // 第七个参数String：对查询的结果进行排序
        Cursor cursor = db.query(Tables.TABLE_USER,
        		new String[] {TableUser.NAME, TableUser.PASSWORD, TableUser.PHONE},
        		TableUser.NAME + "=? and " + TableUser.PASSWORD + "=?", 
        		new String[] {name, pwd},
        		null, null, null); 
        while (cursor.moveToNext()) {
            String s_name = cursor.getString(cursor.getColumnIndex(TableUser.NAME));
            String s_pwd = cursor.getString(cursor.getColumnIndex(TableUser.PASSWORD));  
            String s_phone = cursor.getString(cursor.getColumnIndex(TableUser.PHONE));  
            user = new User(s_name, s_pwd, s_phone);
        }
        cursor.moveToFirst();
        db.close();
		return user;
	}
	
	@Override
	public User getUser(String name) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mDBHelper.getReadableDatabase();
		User user = null;
        Cursor cursor = db.query(Tables.TABLE_USER, 
        		new String[] {TableUser.NAME, TableUser.PASSWORD, TableUser.PHONE},
        		TableUser.NAME + "=?", 
        		new String[] {name},
        		null, null, null);  
        while (cursor.moveToNext()) {
            String s_name = cursor.getString(cursor.getColumnIndex(TableUser.NAME));
            String s_pwd = cursor.getString(cursor.getColumnIndex(TableUser.PASSWORD)); 
            String s_phone = cursor.getString(cursor.getColumnIndex(TableUser.PHONE));  
            user = new User(s_name, s_pwd, s_phone);
        }
        cursor.moveToFirst();
        db.close();
		return user;
	}
	@Override
	public boolean addUser(String name, String pwd, String phone) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(TableUser.NAME, name);
		values.put(TableUser.PASSWORD, pwd);
		values.put(TableUser.PHONE, phone);
		long id = db.insert(Tables.TABLE_USER, null, values);
		return (id != -1);
	}
	@Override
	public boolean delUser(String name) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		String whereClause = TableUser.NAME + "=?";
		String[] whereArgs = new String[]{name};
		db.delete(Tables.TABLE_USER, whereClause, whereArgs);
		return false;
	}
	
	

}
